package com.jml.juc.lock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class Lock3Demo {

    public static void main(String[] args) {
        ReentrantLock lock = new ReentrantLock();

        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"\t 阻塞...");
            lock.lock();
            try{
                System.out.println(Thread.currentThread().getName()+"\t 获取...");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
                System.out.println(Thread.currentThread().getName()+"\t 释放...");
            }
        }, "I").start();


        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(9);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"\t 阻塞...");
            lock.lock();
            try{
                System.out.println(Thread.currentThread().getName()+"\t 获取...");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
                System.out.println(Thread.currentThread().getName()+"\t 释放...");
            }
        }, "H").start();

        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(8);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"\t 阻塞...");
            lock.lock();
            try{
                System.out.println(Thread.currentThread().getName()+"\t 获取...");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
                System.out.println(Thread.currentThread().getName()+"\t 释放...");
            }
        }, "G").start();

        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(7);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"\t 阻塞...");
            lock.lock();
            try{
                System.out.println(Thread.currentThread().getName()+"\t 获取...");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
                System.out.println(Thread.currentThread().getName()+"\t 释放...");
            }
        }, "F").start();

        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(6);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"\t 阻塞...");
            lock.lock();
            try{
                System.out.println(Thread.currentThread().getName()+"\t 获取...");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
                System.out.println(Thread.currentThread().getName()+"\t 释放...");
            }
        }, "E").start();

        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"\t 阻塞...");
            lock.lock();
            try{
                System.out.println(Thread.currentThread().getName()+"\t 获取...");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
                System.out.println(Thread.currentThread().getName()+"\t 释放...");
            }
        }, "D").start();

        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(4);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"\t 阻塞...");
            lock.lock();
            try{
                System.out.println(Thread.currentThread().getName()+"\t 获取...");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
                System.out.println(Thread.currentThread().getName()+"\t 释放...");
            }
        }, "C").start();

        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"\t 阻塞...");
            lock.lock();
            try{
                System.out.println(Thread.currentThread().getName()+"\t 获取...");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
                System.out.println(Thread.currentThread().getName()+"\t 释放...");
            }
        }, "B").start();

        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"\t 阻塞...");
            lock.lock();
            try{
                System.out.println(Thread.currentThread().getName()+"\t 获取...");
                TimeUnit.SECONDS.sleep(15);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
                System.out.println(Thread.currentThread().getName()+"\t 释放...");
            }
        }, "A").start();

    }
}
